﻿//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    Crolengi, this program (a part of program) is gameengine based on C++\Directx
//    Copyright (C) 2008 Pashinin Anton Alekseevich (Crol)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//    (INCLUDING NEGLIGENCE OR OTHERWISE) 
//    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//    Contacts: 
//    Mail: crolengi@gmail.com
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


#ifndef CLREngi_ini_read_h
#define CLREngi_ini_read_h

#include <stdio.h>
#include "CLREngi_main_class.h"
//#include "CRLEngi_trees_params_lib\\CRLEngi_trees_params.h"
//#include "param_trees_lib\\param_trees.h"
#include "..\\import\\CRLEngi_strings_lib\\CRLEngi_strings.h"
#include "..\\import\\param_trees_lib\\param_trees.h"
#include <malloc.h>


#pragma warning (disable:4996)
//////////////////////////////////////////////////////////////////////////////////////////////////////000
//
// Кдасс читальщика файлов cini
//
//////////////////////////////////////////////////////////////////////////////////////////////////////000
class CRLEngi_cIniReader
{
public:
	DynamicRow a;
	
DynamicRow ReadIni(CRLEngi_string fileName)
{
	DynamicRow INIPARAMETERS;
	int id;int id2;

	INIPARAMETERS.fresh();
	id2=INIPARAMETERS.AddToId(0,true,"параметры инициализации");

	FILE *inifile;
    char name[1024];
	strcpy(name,fileName.getChars());
	fopen_s(&inifile,name,"r"); 
	if (inifile!=NULL)
	{
		char buff[255];
		CRLEngi_string line;
		CRLEngi_string command;
		CRLEngi_string parameter;
		while (!feof(inifile))
		{
			// читаем очередную строку
			fgets(buff,255,inifile);
			// удаление символа '\10' на конце считанной строки
			int len = strlen(buff);
			if (len>1) buff[len-1]='\0';

			line=buff;
			//пробуем считать блок [command]=parameter
			if (line.getCommandBlock(&command,&parameter)==true)
			{
				//PARE.Add(command);
				id=INIPARAMETERS.AddToId(id2,true,command);
				INIPARAMETERS.AddToIdSeparated(id,parameter,',');				
				//PARE.fresh();
			}

		}
		return INIPARAMETERS;
	}
	else 
	{
		CRLEngi_string Error;
		Error = "| File loading failed! |";
		Error.add(fileName);Error.add(" | ");

		MessageBox(NULL,Error.getChars(), "ERROR",MB_OK|MB_ICONEXCLAMATION);
		return INIPARAMETERS;
	}


}



public:
	CRLEngi_cIniReader(){};
	virtual ~CRLEngi_cIniReader(){};
};

#endif